/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.bundlecontroller.manager;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.net.URL;

import javax.swing.UIManager;

import jp.co.withone.osgi.gadget.bundlecontroller.balloon.BalloonViewService;
import jp.co.withone.osgi.gadget.bundlecontroller.balloon.impl.BallloonViewServiceImpl;
import jp.co.withone.osgi.gadget.bundlecontroller.viewer.impl.TrayIconMouseAdapter;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class Activator implements BundleActivator {

    /** Tray Icon Resource Path */
    private static final String RESOURCE_TRAY_ICON = "/jp/co/withone/osgi/gadget/bundlecontroller/resource/trayIcon.png";

    /** Tray Icon */
    private TrayIcon trayIcon = null;
    
    /** ViewManager */
    private ViewManager manager = null;
    
    /** Service Registration BalloonViewService */
    private ServiceRegistration balloonViewServiceReg = null;
    
    /*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
     * )
     */
    public void start(BundleContext context) throws Exception {
        if (SystemTray.isSupported()) {
            manager = new ViewManager(context);
            manager.init();
            createTrayIcon();
            addTrayIcon();
            BalloonViewService balloonViewService = new BallloonViewServiceImpl(trayIcon);
            balloonViewServiceReg = context.registerService(BalloonViewService.class.getName(), balloonViewService, null);
        } else {
            new Exception("systemTray unSupport !");
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        if (SystemTray.isSupported()) {
            if (manager != null) {
                manager.end();
            }
            deleteTrayIcon();
            if (balloonViewServiceReg != null) {
                balloonViewServiceReg.unregister();
            }
        }
    }
    
    private void createTrayIcon(){
        final URL imagePath = this.getClass().getResource(RESOURCE_TRAY_ICON);
        final Image image = Toolkit.getDefaultToolkit().getImage(imagePath);
        trayIcon = new TrayIcon(image);
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip("Gadget on OSGi");
        trayIcon.addMouseListener(new TrayIconMouseAdapter(manager));
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void addTrayIcon() throws AWTException{
        final SystemTray tray = SystemTray.getSystemTray();
//        boolean isStay = false;
//        for (TrayIcon stayIcon : tray.getTrayIcons()) {
//            if(stayIcon.equals(trayIcon)){
//                isStay = true;
//                break;
//            }
//        }
//        if (!isStay) {
            tray.add(trayIcon);
//        }
    }
    
    private void deleteTrayIcon() throws AWTException{
        final SystemTray tray = SystemTray.getSystemTray();
        if (trayIcon != null) {
            tray.remove(trayIcon);
        }
    }
}
